package com.example.downloaddemo;


import android.content.Context;
import android.view.View;

import com.example.downloaddemo.base.CommonAdapter;
import com.example.downloaddemo.base.ViewHolder;
import com.example.downloaddemo.downloader.DownloadEntry;
import com.example.downloaddemo.downloader.DownloadManager;

import java.util.List;

public class ListAdapter extends CommonAdapter<DownloadEntry> {


    private final DownloadManager mDownloadManager;

    public ListAdapter(Context context, List mDatas, int itemLayoutId) {
        super(context, mDatas, itemLayoutId);
        mDownloadManager = DownloadManager.getInstance(context);
    }

    @Override
    public void convert(ViewHolder helper, final DownloadEntry item) {
        helper.setText(R.id.tv_pro, item.getProgress() + "进度" + item.getName()+(item.status==DownloadEntry.DownloadStatus.waiting?"等待中":""));
        helper.getView(R.id.tv_down).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.status == DownloadEntry.DownloadStatus.downloading) {
                    mDownloadManager.cancel(item);
                } else {
                    mDownloadManager.add(item);
                }
            }
        });
    }
}
